package com.atguigu.tingshu.user.context;

import com.atguigu.tingshu.user.stragegy.UpdateUserPaidRecordStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * UpdateUserPaidRecordStrategyContext
 *
 * @author Petrichor
 * @version 2024/01/08 23:03
 **/
@Component
public class UpdateUserPaidRecordStrategyContext implements ApplicationContextAware {

    private static final Map<String, UpdateUserPaidRecordStrategy> PAYE_INFO_STRATGY_MAP = new HashMap<>();

    public static UpdateUserPaidRecordStrategy getUpdateUserPaidRecordStrategy(String itemType){
        return PAYE_INFO_STRATGY_MAP.get(itemType);
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, UpdateUserPaidRecordStrategy> beansOfType = applicationContext.getBeansOfType(UpdateUserPaidRecordStrategy.class);
        beansOfType.values().stream().forEach(updateUserPaidRecordStrategy -> {
            String itemType = updateUserPaidRecordStrategy.getItemType();
            PAYE_INFO_STRATGY_MAP.put(itemType , updateUserPaidRecordStrategy) ;
        });
    }
}


